package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.core.domain.entity.SysUser;
import cn.sfrog.common.enums.UserTypeEnum;
import cn.sfrog.common.utils.DateUtils;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizUserProfessionRecord;
import cn.sfrog.project.biz.admin.domain.vo.BizUserProfessionRecordFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserProfessionRecordQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserProfessionRecordVO;
import cn.sfrog.project.biz.admin.mapper.BizUserProfessionRecordMapper;
import cn.sfrog.project.biz.admin.service.IBizUserProfessionRecordService;
import cn.sfrog.project.biz.web.vo.index.AppAuthRecordResultVO;
import cn.sfrog.project.biz.web.vo.index.AppDoctorProfessionalVO;
import cn.sfrog.project.biz.web.vo.index.AppJobAuthResultVO;
import cn.sfrog.system.domain.vo.BizAuditVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.mapper.SysDictDataMapper;
import cn.sfrog.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 用户职业资质认证Service业务层处理
 */
@Service
public class BizUserProfessionRecordServiceImpl extends ServiceImpl<BizUserProfessionRecordMapper, BizUserProfessionRecord> implements IBizUserProfessionRecordService {
    @Resource
    private BizUserProfessionRecordMapper bizUserProfessionRecordMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;
    /**
     * 查询用户职业资质认证
     *
     * @param id 用户职业资质认证主键
     * @return 用户职业资质认证
     */
    @Override
    public BizUserProfessionRecordVO selectBizUserProfessionRecordById(Long id) {
        return bizUserProfessionRecordMapper.selectBizUserProfessionRecordById(id);
    }

    /**
     * 查询用户职业资质认证列表
     *
     * @param query 用户职业资质认证
     * @return 用户职业资质认证
     */
    @Override
    public List<BizUserProfessionRecordVO> selectBizUserProfessionRecordList(BizUserProfessionRecordQueryVO query) {
        if(query.getDateValue() != null){
            query.setStartTime(query.getDateValue()[0] + " 00:00:00");
            query.setEndTime(query.getDateValue()[1] + " 23:59:59");
        }
        return bizUserProfessionRecordMapper.selectBizUserProfessionRecordList(query);
    }

    /**
     * 新增用户职业资质认证
     *
     * @param form 用户职业资质认证
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizUserProfessionRecord(BizUserProfessionRecordFormVO form) {
        BizUserProfessionRecord bizUserProfessionRecord = new BizUserProfessionRecord();
        BeanUtils.copyProperties(form, bizUserProfessionRecord);
        bizUserProfessionRecord.setCreateTime(new Date());
        bizUserProfessionRecord.setCreateBy(SecurityUtils.getUsername());
        bizUserProfessionRecord.setUpdateTime(new Date());
        bizUserProfessionRecord.setUpdateBy(SecurityUtils.getUsername());
        return bizUserProfessionRecord.insert();
    }

    /**
     * 修改用户职业资质认证
     *
     * @param form 用户职业资质认证
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizUserProfessionRecord(BizUserProfessionRecordFormVO form) {
        BizUserProfessionRecord bizUserProfessionRecord = new BizUserProfessionRecord();
        BeanUtils.copyProperties(form, bizUserProfessionRecord);
        bizUserProfessionRecord.setUpdateTime(new Date());
        bizUserProfessionRecord.setUpdateBy(SecurityUtils.getUsername());
        return bizUserProfessionRecord.updateById();
    }

    /**
     * 批量删除用户职业资质认证
     *
     * @param ids 需要删除的用户职业资质认证主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizUserProfessionRecordByIds(Long[] ids) {
            return BizUserProfessionRecord.DAO.delete(Wrappers.lambdaQuery(BizUserProfessionRecord.class).in(BizUserProfessionRecord::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return BizUserProfessionRecord.DAO.update(Wrappers.lambdaUpdate(BizUserProfessionRecord.class).in(BizUserProfessionRecord::getId, (Object[]) vo.getIds()).set(BizUserProfessionRecord::getStatus, vo.getStatus()));
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean audit(BizAuditVO auditVO) {
        LambdaUpdateWrapper<BizUserProfessionRecord> updateWrapper = new LambdaUpdateWrapper<BizUserProfessionRecord>()
                .eq(BizUserProfessionRecord::getId,auditVO.getId())
                .set(BizUserProfessionRecord::getAuditStatus,auditVO.getAuditStatus())
                .set(BizUserProfessionRecord::getRemark,auditVO.getRemark())
                .set(BizUserProfessionRecord::getUpdateBy,SecurityUtils.getUsername())
                .set(BizUserProfessionRecord::getUpdateTime, DateUtils.getNowDate());
        if(Constants.AUDIT_STATUS_PASS.equals(auditVO.getAuditStatus())){
            //审核通过将认证状态修改为已认证
            updateWrapper.set(BizUserProfessionRecord::getStatus,BizUserProfessionRecord.VERIFIED);
            //更新用户表
            BizUserProfessionRecord record = BizUserProfessionRecord.DAO.selectById(auditVO.getId());
            SysUser sysUser = sysUserMapper.selectUserById(record.getUserId());
            SysUser updateUser = new SysUser();
            updateUser.setUserId(sysUser.getUserId());
            if(UserTypeEnum.ESCORT.getCode().equals(sysUser.getUserType())){
                updateUser.setEscortLevel(record.getProfessionName());
            }else if(UserTypeEnum.CARE_WORKER.getCode().equals(sysUser.getUserType())){
                updateUser.setLevel(record.getProfessionName());
            }else if(UserTypeEnum.DOCTOR.getCode().equals(sysUser.getUserType())){
                updateUser.setLevel(record.getProfessionName());
            }
            updateUser.setHospitalId(record.getHospitalId());
            updateUser.setDepartmentId(record.getDepartmentId());
            sysUserMapper.updateUser(updateUser);
        }
        return BizUserProfessionRecord.DAO.update(updateWrapper);
    }

    @Override
    public List<AppJobAuthResultVO> selectAppJobAuthList(Long userId,String userType) {
        List<AppJobAuthResultVO> jobAuthResultVOList = bizUserProfessionRecordMapper.selectAppJobAuthList(userId,userType);
        for (AppJobAuthResultVO vo:jobAuthResultVOList) {
            Date validEndDate = vo.getValidEndDate();
            if(StringUtils.isNotNull(validEndDate)){
                if(validEndDate.before(new Date())){
                    vo.setStatus("2");
                }
            }
        }
        return jobAuthResultVOList;
    }

    @Override
    public List<AppAuthRecordResultVO> selectAppAuthRecord(Long userId,String userType) {
        return bizUserProfessionRecordMapper.selectAppAuthRecord(userId,userType);
    }

    @Override
    public AppDoctorProfessionalVO selectLastProfession(Long userId) {
        return bizUserProfessionRecordMapper.selectLastProfession(userId);
    }
}
